package main

import (
	"encoding/json"
	"fmt"
	"go.manyji.com/lib/util/convert"
)

type (
	filmParams struct {
		PaginationParam
	}
)

func GetFilmList(filmCategoryId int, args ...filmParams) ([]*FilmDetail, error) {

	defArgs := filmParams{
		PaginationParam{
			PageIndex: 1,
			PageSize:  1000,
		},
	}
	if len(args) > 0 {
		defArgs = args[0]
	}
	repMap := map[string]interface{}{}
	filmData, err := getFilmList(filmCategoryId, defArgs.PageIndex, defArgs.PageSize)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(filmData), &repMap)
	if err != nil {
		return nil, err
	}

	dataMaps, ok := repMap["Data"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	listMaps, ok := dataMaps["list"].([]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	var films []*FilmDetail

	for _, dataMap := range listMaps {
		u := dataMap.(map[string]interface{})

		if u["id"] == 0 {
			continue
		}

		films = append(films, &FilmDetail{
			Id:       convert.Atoi(convert.ToString(u["id"])),
			Name:     convert.ToString(u["name"]),
			Episodes: convert.Atoi(convert.ToString(u["episodes"])),
		})
	}
	return films, nil
}

func GetFilmCategories() ([]*FilmCategoryData, error) {

	repMap := map[string]interface{}{}
	catData, err := getFilmCategoryList()
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal([]byte(catData), &repMap)
	if err != nil {
		return nil, err
	}

	dataMaps, ok := repMap["Data"].([]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	var acts []*FilmCategoryData

	for _, dataMap := range dataMaps {
		u := dataMap.(map[string]interface{})

		if u["id"] == 0 {
			continue
		}

		acts = append(acts, &FilmCategoryData{
			Id:   convert.Atoi(convert.ToString(u["id"])),
			Name: convert.ToString(u["name"]),
		})
	}
	return acts, nil
}

func UnlockFilmEpisode(filmId int, episodes, deviceId string, auto string) error {
	res, err := unlockFilmEpisode(filmId, 0, episodes, deviceId, auto)
	if err != nil {
		return err
	}
	resData := &ApiRep[map[string]interface{}]{}
	err = json.Unmarshal([]byte(res), &resData)
	if err != nil {
		return err
	}
	if resData.Code != 0 {
		return fmt.Errorf(fmt.Sprintf("%v:%v", resData.Code, resData.ErrMsg))
	}

	return nil
}

func GetFilmEpisodeCoins(filmId int, episodes, deviceId string, auto string) (*UnlockEpisodeRsp, error) {

	if filmId == 3 {
		fmt.Println("break")
	}
	repMap := map[string]interface{}{}
	repData, err := unlockFilmEpisode(filmId, 1, episodes, deviceId, auto)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal([]byte(repData), &repMap)
	if err != nil {
		return nil, err
	}

	dataMaps, ok := repMap["Data"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	return &UnlockEpisodeRsp{
		Price: convert.Atoi(convert.ToString(dataMaps["price"])),
		Msg:   convert.ToString(dataMaps["msg"]),
	}, nil
}

func GetFilmDetail(filmId int64, deviceId string) (*GetFilmDetailRsp, error) {

	if filmId == 3 {
		fmt.Println("break")
	}
	repMap := map[string]interface{}{}
	repData, err := getFilmDetail(filmId, deviceId)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal([]byte(repData), &repMap)
	if err != nil {
		return nil, err
	}

	dataMaps, ok := repMap["Data"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%v", repMap["Code"])
	}

	return &GetFilmDetailRsp{
		Id:       int64(convert.Atoi(convert.ToString(dataMaps["id"]))),
		Name:     convert.ToString(dataMaps["name"]),
		Image:    convert.ToString(dataMaps["image"]),
		Episodes: convert.Atoi(convert.ToString(dataMaps["episodes"])),
		Language: convert.ToString(dataMaps["language"]),
		Keywords: convert.ToString(dataMaps["keywords"]),
		Host:     convert.ToString(dataMaps["host"]),
		UnlockedFilmEpisodes: func() []*FilmEpisodesRsp {
			var fes []*FilmEpisodesRsp
			if _, ok := dataMaps["unlocked_film_episodes"].([]interface{}); ok {
				for _, v := range dataMaps["unlocked_film_episodes"].([]interface{}) {
					vv := v.(map[string]interface{})

					fes = append(fes, &FilmEpisodesRsp{
						Id:        int64(convert.Atoi(convert.ToString(vv["id"]))),
						EpisodeNo: convert.Atoi(convert.ToString(vv["episode_no"])),
						Expired:   int64(convert.Atoi(convert.ToString(vv["expired"]))),
					})
				}
			}
			return fes
		}(),
	}, nil
}
